package com.jiashihui.schema_lgorithm.java.socket;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SocketServer {
    public static void main(String[] args) throws Exception {
        // 监听指定的端口
        int port = 55533;
        ServerSocket server = new ServerSocket(port);
        // server将一直等待连接的到来
        System.out.println("server将一直等待连接的到来");

        //如果使用多线程，那就需要线程池，防止并发过高时创建过多线程耗尽资源
        ExecutorService threadPool = Executors.newFixedThreadPool(100);

        while (true) {
            Socket socket = server.accept();

            // 读超时设置
            // socket.setSoTimeout(2000);

            Runnable runnable=()->{
                try {
                    // 建立好连接后，从socket中获取输入流，并建立缓冲区进行读取
                    InputStream inputStream = socket.getInputStream();
                    byte[] bytes = new byte[1024];
                    int len;
                    StringBuilder sb = new StringBuilder();
                    while ((len = inputStream.read(bytes)) != -1) {
                        // 注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
                        sb.append(new String(bytes, 0, len, "UTF-8"));
                    }
                    System.out.println("get message from client: " + sb);
                    inputStream.close();
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
            threadPool.submit(runnable);
        }
    }
//    public static void main(String[] args) throws Exception {
//        // 监听指定的端口
//        int port = 55533;
//        ServerSocket server = new ServerSocket(port);
//
//        // server将一直等待连接的到来
//        System.out.println("server将一直等待连接的到来");
//        Socket socket = server.accept();
//        // 建立好连接后，从socket中获取输入流，并建立缓冲区进行读取
//        InputStream inputStream = socket.getInputStream();
//        byte[] bytes;
//        // 因为可以复用Socket且能判断长度，所以可以一个Socket用到底
//        while (true) {
//            // 首先读取两个字节表示的长度
//            int first = inputStream.read();
//            //如果读取的值为-1 说明到了流的末尾，Socket已经被关闭了，此时将不能再去读取
//            if(first==-1){
//                break;
//            }
//            int second = inputStream.read();
//            int length = (first << 8) + second;
//            // 然后构造一个指定长的byte数组
//            bytes = new byte[length];
//            // 然后读取指定长度的消息即可
//            inputStream.read(bytes);
//            System.out.println("get message from client: " + new String(bytes, "UTF-8"));
//        }
////        byte[] bytes = new byte[1024];
////        int len;
////        StringBuilder sb = new StringBuilder();
////        while ((len = inputStream.read(bytes)) != -1) {
////            //注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
////            sb.append(new String(bytes, 0, len,"UTF-8"));
////        }
////        System.out.println("get message from client: " + sb);
//
//        // 发送消息
////        OutputStream outputStream = socket.getOutputStream();
////        outputStream.write("Hello Client, I get the message.".getBytes("UTF-8"));
//
//        inputStream.close();
////        outputStream.close();
//        socket.close();
//        server.close();
//    }
}
